package com.jeasonzhao.commons.configuration;

import com.jeasonzhao.commons.basic.Pair;
import com.jeasonzhao.commons.basic.StringCollection;
import com.jeasonzhao.commons.utils.Algorithms;

public class CommandLineArguments
{
    public static final String Default_ParameterSectionKey = "-";
    private java.util.ArrayList<Pair<String,StringCollection>> m_options = new java.util.ArrayList<Pair<String,StringCollection>>();
    private String m_myKey = Default_ParameterSectionKey;
    public CommandLineArguments(String[] commandLineOptions)
        throws CommandLineException
    {
        this(commandLineOptions,null);
    }

    public CommandLineArguments(String strCommandLine)
        throws CommandLineException
    {
        if(null == strCommandLine)
        {
            return;
        }
        java.util.ArrayList<String> stringAry = new java.util.ArrayList<String>();
        for(String strOptionPart : strCommandLine.split("\\s"))
        {
            if(strOptionPart.trim().length() > 0)
            {
                stringAry.add(strOptionPart);
            }
        }
        String[] ar = new String[stringAry.size()];
        stringAry.toArray(ar);
        this.init(ar);
    }

    public CommandLineArguments(String[] argvs,String strParameterPrefix)
        throws CommandLineException
    {
        m_myKey = Algorithms.isEmpty(strParameterPrefix) ? Default_ParameterSectionKey : m_myKey;
        init(argvs);
    }

    private void init(String[] commandLineOptions)
        throws CommandLineException
    {
        for(int nParameter = 0;null != commandLineOptions && nParameter < commandLineOptions.length;nParameter++)
        {
            String strCurrent = commandLineOptions[nParameter];
            if(Algorithms.isEmpty(strCurrent))
            {
                continue;
            }
            strCurrent = strCurrent.trim();
            if(isParameterSection(strCurrent))
            {
                StringCollection coll = new StringCollection();
                for(nParameter++;nParameter < commandLineOptions.length;nParameter++)
                {
                    String strParameter = commandLineOptions[nParameter];
//                    System.out.println(nParameter+"?????"+strParameter);
                    if(Algorithms.isEmpty(strParameter))
                    {
                        continue;
                    }
                    if(isParameterSection(strParameter))
                    {
                        nParameter--;
                        break;
                    }
//                    System.out.println(nParameter+"  "+strCurrent+"....."+strParameter);
                    coll.add(strParameter);
                }
                addParameter(strCurrent,coll);
            }
            else
            {
                addParameter(strCurrent,null);
            }
        }
    }

    protected void addParameter(String strKey,StringCollection coll)
        throws CommandLineException
    {
        if(Algorithms.isEmpty(strKey))
        {
            return;
        }
        Pair<String,StringCollection> p = new Pair<String,StringCollection>();
        p.set(strKey,coll);
        m_options.add(p);
    }

    protected boolean isParameterSection(String s)
    {
        return null != s && s.startsWith(this.m_myKey);
    }

    protected String getRealParameterName(String s)
    {
        return s == null || s.trim().length() < 1 || s.startsWith(this.m_myKey)
            ? s
            : m_myKey + s.trim();
    }

    public boolean isContains(String key)
    {
        key = getRealParameterName(key);
        for(Pair<String,StringCollection> p : this.m_options)
        {
            if(p != null && p.getFirst().equalsIgnoreCase(key))
            {
                return true;
            }
        }
        return false;
    }

    public String getString(String key)
    {
        key = getRealParameterName(key);
        for(Pair<String,StringCollection> p : this.m_options)
        {
            if(p != null && p.getFirst().equalsIgnoreCase(key))
            {
                StringCollection coll = p.getSecond();
                return null == coll || coll.size() < 1 ? null : coll.get(0);
            }
        }
        return null;
    }

    public StringCollection getParameters(String key)
    {
        key = getRealParameterName(key);
        for(Pair<String,StringCollection> p : this.m_options)
        {
            if(p != null && p.getFirst().equalsIgnoreCase(key))
            {
                return p.getSecond();
            }
        }
        return null;
    }

}
